package com.xes.apps.ams.scheduler.service;

import java.util.Calendar;

import javax.inject.Inject;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import com.xes.apps.ams.client.rd60n.jiecard.cn.RD60N;
import com.xes.apps.ams.config.ServerConfig;
import com.xes.apps.ams.scheduler.Scheduler;

@Component
public class RfMachineSchedulerService implements Scheduler {
	@Inject
	private ServerConfig config;
	private static final Logger log = LogManager.getLogger(RfMachineSchedulerService.class);

	@Override
	@Scheduled(cron = "0 */30 * * * *")		// peer 30 minuts
	public void execute() {
		StopWatch stopWatch = new StopWatch("校正读卡机时间服务:");
		for (String ip : config.getCardReaderIp()) {
			stopWatch.start("读卡机ip[:" + ip + "]");
			RD60N dev = new RD60N();
			dev.rf_open(ip, "UDP");

			Calendar c = Calendar.getInstance();
			short[] datetime = { (short) (c.get(Calendar.YEAR)), (short) (c.get(Calendar.MONTH) + 1),
					(short) (c.get(Calendar.DATE)), (short) (c.get(Calendar.HOUR_OF_DAY)),
					(short) (c.get(Calendar.MINUTE)), (short) (c.get(Calendar.SECOND)) };

			log.info("正在校正读卡器 :{},时间:{}", ip, c.getTime().toString());

			dev.rf_set_datetime(datetime);
			dev.rf_beep((byte) 5);
			dev.rf_close();
			stopWatch.stop();
		}
//		log.info(stopWatch.prettyPrint());
	}
}
